<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class AdminCase extends BaseModel
{
    //定义表名 案列表
    protected $name = 'longbing_card_decorate_case';
    protected $append = [
        'area_text',
        'style_text',
        'housewear_text',
        'designer_text'

    ];
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }

    /**
     * User: chenniang
     * Date: 2019-10-21 16:47
     * @param $value
     * @param $data
     * @return mixed
     * descption:设计师
     */
    public function getDesignerTextAttr($value,$data){
        $dis[] = ['is_staff','=',1];
        $dis[] = ['fans_id','=',$data['designer']];
        $designer = Db::name('longbing_card_user_info')->where($dis)->value('name');
        return $designer;
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取地区名字
     */
    public function getAreaTextAttr($value,$data){
        $area_model = new AdminArea();
        return $area_model->areaTitle(['id'=>$data['area']]);
    }

    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取风格名字
     */
    public function getStyleTextAttr($value,$data){
        $style_model = new AdminStyle();
        return $style_model->styleTitle(['id'=>$data['style']]);
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:57
     * @param $value
     * @param $data
     * @return mixed
     * descption:居室名字
     */
    public function getHousewearTextAttr($value,$data){
        $wear_model = new AdminHousewear();
        return $wear_model->housewearTitle(['id'=>$data['housewear']]);
    }

    /**
     * User: chenniang
     * Date: 2019-09-18 10:18
     * @param $value
     * @return array|mixed
     * descption:转换vr封面图
     */
    public function getCoverAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:$img;
    }

    /**
     * User: chenniang
     * Date: 2019-10-30 17:13
     * @param $value
     * @param $data
     * @return array
     * descption:转数组
     */
    public function getAreaAttr($value,$data){
        $model = new AdminArea();
        return $model->reset($data['area']);

    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgsAttr($value){
        $img = transImages([$value],['imgs'],',');
        return is_array($img[0])?$img[0]:[];
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 案例列表
     */
    public function caseList($dis,$page = 10){
        $data = $this->where($dis)->status(-1)->order('sort desc,create_time desc')->paginate($page)->toArray();
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * 添加案例
     */
    public function caseAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = $this->insert($data);
        return $res;
    }


    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑案例
     */
    public function caseUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }


    /**
     * @param $dis
     * @return AdminCate
     * 案例详情
     */
    public function caseInfo($dis){
        $data = $this->where($dis)->status(-1)->find();
        return !empty($data)?$data->toArray():$data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 16:36
     * @return void
     * descption:转换数据
     */
    public function changeData($data){
        if (isset($data['area'])) {
            $data['area'] = !empty($data['area'])?array_pop($data['area']):'';
        }
        if (isset($data['imgs'])) {
            $data['imgs'] = !empty($data['imgs'])?implode($data['imgs'],','):'';
        }
        return $data;
    }




}